import { request } from 'umi';
import { IdeEntity } from '@/pages/Ide/entities';
import { CREATE, DELETE, UPDATE, LIST } from '@/constant';

const route = '/ide';

export async function getIdeUrl(id: string | undefined) {
  return await request<IdeEntity>(`${route}/get_ide_url?id=${id}`, {
    method: 'GET',
  });
}

export async function getIde(id: string | undefined) {
  return await request<IdeEntity>(`${route}/get_entity?id=${id}`, {
    method: 'GET',
  });
}

export async function startIde(id: string | undefined) {
  return await request<IdeEntity>(`${route}/start?id=${id}`, {
    method: 'GET',
  });
}

export async function stopIde(id: string | undefined) {
  return await request<IdeEntity>(`${route}/stop?id=${id}`, {
    method: 'GET',
  });
}

export async function listIde(
  params: {
    current?: number;
    pageSize?: number;
  },
  options?: { [key: string]: any },
) {
  return request<{
    data: IdeEntity[];
    total?: number;
    success?: boolean;
  }>(`${route}${LIST}`, {
    method: 'GET',
    params: {
      ...params,
    },
    ...options,
  });
}

export async function addIde(
  data: { [key: string]: any },
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`${route}${CREATE}`, {
    data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function updateIde(
  data: { [key: string]: any },
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`${route}${UPDATE}`, {
    data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function deleteIde(data: string, options?: { key: string[] }) {
  return request<Record<string, any>>(`${route}${DELETE}?ids=${data}`, {
    method: 'GET',
    ...(options || {}),
  });
}
